package siruna.WE.core.events;

import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import siruna.WE.core.model.EventHandler;
import siruna.WE.core.model.EventList;
import siruna.WE.core.model.VisualBlock;

public class EventListFactory {
	EventListener eventhandler;
	HashMap<String, EventList> eventLists;
	private String widgetName;

	public EventListFactory(EventListener eventhandler, String widgetName) {
		this.eventhandler = eventhandler;
		this.widgetName = widgetName;
		eventLists = new HashMap<String, EventList>();
	}

	public HashMap<String, EventList> makeEventLists(NodeList vis) {
		for (int i = 0; i < vis.getLength(); i++) {
			Node n = vis.item(i);
			EventList eventList = createEventList(n);

			eventLists.put(n.getAttributes().getNamedItem("Id").getNodeValue(),
					eventList);

			System.out.println("Event toegevoegd: "
					+ n.getAttributes().getNamedItem("Id").getNodeValue());
		}

		return eventLists;
	}

	private EventList createEventList(Node n) {
		EventList el = new EventList();
		el.setId(n.getAttributes().getNamedItem("Id").getNodeValue());

		
		NodeList list = n.getChildNodes();
		for (int i = 0; i < list.getLength(); i++) {
			if (list.item(i).getNodeType() == Document.ELEMENT_NODE) {
				EventHandler e = new EventHandler();
				NamedNodeMap att = list.item(i).getAttributes();
				e.setId(att.getNamedItem("Id").getNodeValue());
				att.removeNamedItem("Id");
				for (int j = 0; j < att.getLength(); j++) {
					e.setAttribute(att.item(j).getNodeName(), att.item(j)
							.getNodeValue());
				}
				el.addEventHandler(e);
			}
		}
		
		el.setWidgetName(widgetName);

		return el;

	}

}
